cmake_minimum_required(VERSION 3.6)
project(nexus)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")

find_package(gflags REQUIRED)

# Fix libicudata error while linking folly
find_package(Boost COMPONENTS regex REQUIRED)
find_package(folly REQUIRED)

find_package(Protobuf CONFIG REQUIRED)
find_package(gRPC CONFIG REQUIRED)
add_library(nexus_grpc_proto OBJECT "${CMAKE_SOURCE_DIR}/nexus/proto/nexus.proto")

target_link_libraries(nexus_grpc_proto PUBLIC protobuf::libprotobuf gRPC::grpc++)

set(PROTO_GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/proto-gen")
file(MAKE_DIRECTORY ${PROTO_GEN_DIR})

protobuf_generate(
    TARGET nexus_grpc_proto
    PROTOC_OUT_DIR "${PROTO_GEN_DIR}")

protobuf_generate(
    TARGET nexus_grpc_proto
    LANGUAGE grpc
    GENERATE_EXTENSIONS .grpc.pb.h .grpc.pb.cc
    PLUGIN "protoc-gen-grpc=\$<TARGET_FILE:gRPC::grpc_cpp_plugin>"
    PROTOC_OUT_DIR "${PROTO_GEN_DIR}")

include_directories(
  ${PROTO_GEN_DIR}
  ${CMAKE_SOURCE_DIR}/nexus
)

add_executable(${PROJECT_NAME}
  nexus/aggregator/Aggregator.cpp
  nexus/atomizer/Atomizer.cpp
  nexus/common/Device.cpp
  nexus/common/EventBase.cpp
  nexus/dataplane/Model.cpp
  nexus/dataplane/ModelManager.cpp
  nexus/model-updater/ModelUpdater.cpp
  nexus/normalizer/Normalizer.cpp
  nexus/server/Server.cpp
  nexus/Main.cpp
)

target_link_libraries(${PROJECT_NAME}
  ${GFLAGS_LIBRARIES}
  ${FOLLY_LIBRARIES}
  gRPC::grpc++
  nexus_grpc_proto
  tbb
  nanobdd
)